package com.manager.sms;

import com.sms.cpa.CMoReq;
import com.sms.cpa.CReportReq;
import com.sms.cpa.CSendClusterRet;
import com.sms.cpa.CSendRet;
import com.sms.impl.Queue;
import com.sms.pub.MyRunnable;

public class GetReport extends MyRunnable {
	private Queue queue;

	public GetReport(Queue queue) {
		super("接收线程");
		this.queue = queue;
		resetIdle();
		setMaxIdle(300);
	}

	public void run() {
		System.out.println(getName() + " 线程已开启");
		bStop = false;
		resetIdle();
		while (!bStop) {
			resetIdle();
			Object obj = null;
			if (!queue.isEmpty()) {
				obj = queue.pop();
				if (obj instanceof CSendRet) {
					CSendRet send = (CSendRet) obj;
					String reqid = send.sReqID; // reqid用于状态报告返回时相匹配
					int status = send.bStatus; // 发送结果
					String msg = send.sMsg; // 信息内容
					// 应答和发送短信进行匹配,根据sequence可以确认应答和所发送的短信相匹配
					int sequence = Integer.parseInt(send.sSerialNo);

					int result = 1;
					String sendRet = "";
					switch (status) {
					case 0x01:
						result = 1;
						sendRet = status + "*成功提交";
						break;
					case 0x32:
						result = 4;
						sendRet = status + "*失败-AGENT断线";
						break;
					case 0x33:
						result = 4;
						sendRet = status + "*失败-SMSC断线";
						break;
					case 0x34:
						result = 4;
						sendRet = status + "*失败-AGENT与SMSC之间断线";
						break;
					case 0x35:
						result = 7;
						sendRet = status + "*其他原因提交失败";
						break;
					case 0x36:
						result = 7;
						sendRet = status + "*费用不足";
						break;
					case 0x37:
						result = 7;
						sendRet = status + "*无MT权限";
						break;
					case 0x38:
						result = 7;
						sendRet = status + "*无可用的MT通道";
						break;
					case 0xFF:
						result = 7;
						sendRet = status + "*错误的指令格式";
						break;
					}
					System.out.println("-单发发送结果:sequence(" + sequence
							+ ")，reqid(" + reqid + ")" + result
							+ "(4、7失败，其他成功): " + sendRet);
					// do something 例如数据库操作

				} else if (obj instanceof CSendClusterRet) {
					// 群发因未设置唯一序列号，返回的应答包和状态报告无法匹配，所以不做处理
					CSendClusterRet send = (CSendClusterRet) obj;
					String reqid = send.sReqID; // reqid用于状态报告返回时相匹配
					int status = send.bStatus; // 发送结果
					String msg = send.sMsg; // 信息内容

					int result = 1;
					String sendRet = "";
					switch (status) {
					case 0x01:
						result = 1;
						sendRet = status + "*成功提交";
						break;
					case 0x32:
						result = 4;
						sendRet = status + "*失败-AGENT断线";
						break;
					case 0x33:
						result = 4;
						sendRet = status + "*失败-SMSC断线";
						break;
					case 0x34:
						result = 4;
						sendRet = status + "*失败-AGENT与SMSC之间断线";
						break;
					case 0x35:
						result = 7;
						sendRet = status + "*其他原因提交失败";
						break;
					case 0x36:
						result = 7;
						sendRet = status + "*费用不足";
						break;
					case 0x37:
						result = 7;
						sendRet = status + "*无MT权限";
						break;
					case 0x38:
						result = 7;
						sendRet = status + "*无可用的MT通道";
						break;
					case 0xFF:
						result = 7;
						sendRet = status + "*错误的指令格式";
						break;
					}
					System.out.println("-群发发送结果:reqid(" + reqid + ")"
							+ result + "(4、7失败，其他成功): " + sendRet);
					// do something 例如数据库操作

				} else if (obj instanceof CMoReq) {
					CMoReq mo = ((CMoReq) obj);
					String moid = mo.sMoid; // MO 记录ID
					String timeSend = mo.sTimeSend;// 发送时间
					String timeLand = mo.sTimeLand;// 收到时间
					String from = mo.sFrom; // 源手机号码
					String target = mo.sTarget; // 目的手机号码
					String service = mo.sService; // 业务类别
					String message = new String(mo.bMessage); // 消息内容

					System.out.println("-上行信息,手机号码:" + from + ", 扩展码:"
							+ target + ", 内容:" + message);
					// do something 例如数据库操作

				} else if (obj instanceof CReportReq) {
					CReportReq report = ((CReportReq) obj);
					String reqID = report.sReqID;// // reqID(应答中返回的)进行状态报告匹配
					String status = String.valueOf(report.bStatus); // 发送结果
					String msg = report.sMsg; // 消息内容
					String phone = report.sPhone;

					System.out.println("-状态报告:reqid(" + reqID + ") "
							+ status + "(4、7失败，其他成功): " + msg + ", phone: "
							+ phone);
					// do something 例如数据库操作
				}
			}

			sleep(100);
		}
		System.out.println(getName() + " 线程已关闭");
		end();
	}

	public void end() {
	}

}
